// 引入模块依赖
const fs = require('fs');
const path = require('path');
const jwt = require('jsonwebtoken');
// 创建 token 类
class Jwt {
  constructor(data) {
    this.data = data;
    this.cert = 'zhangheiwa'
  }

  //生成token
  generateToken() {
    let data = this.data;
    let token = jwt.sign(
      data,
      this.cert,
      {
        expiresIn: 2592000 //单位是秒
      }
    )
    return token;
  }

  // 校验token
  verifyToken() {
    let token = this.data;
    try {
      let res = jwt.verify(token, this.cert);
      return res;
    }
    catch (ex) {
      return null
    }
  }
}

module.exports = Jwt;
